'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 用户相关
 * @author sundbcn@gmail.com (sun debing)
 */

var async = require('async');
var _ = require('lodash');
var Code = require('../../../consts/code');
var PlayerManager = require('../../../libs/playerManager');
var roleService = require('../../../services/roleService');
var mailService = require('../../../services/mailService');
var roleModel = require('../../../models/roleModel');
var moment = require('moment');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 禁言
 */
Handler.prototype.banSend = function (msg, session, next) {
    var role_id = msg.query.role_id;
    var ban_send_time = msg.query.ban_send_time;
    var ban_send_state = msg.query.ban_send_state;
    ban_send_time = ban_send_time || 0;

    if (!role_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(['role', 'task', 'daily_task', 'achieve', 'sevenDayActivity', 'accrued'], cb);
            },
            function (cb) {
                var role_model = player_manager.role;

                // 禁言
                var ban_time = moment().unix() + (+ban_send_time);
                role_model.banSend(ban_time);

                // 永久禁言
                if (ban_send_state > 0) {
                    role_model.setPermanentBanSend();
                }

                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK
            });
        }
    );
};

/**
 * 解除禁言
 */
Handler.prototype.openBanSend = function (msg, session, next) {
    var role_id = msg.query.role_id;
    if (!role_id) {
        return next(null, {
            code: Code.PARAM_ERROR
        });
    }
    var player_manager = new PlayerManager(role_id);
    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(['role', 'task', 'daily_task', 'achieve', 'sevenDayActivity', 'accrued'], cb);
            },
            function (cb) {
                var role_model = player_manager.role;
                role_model.openBanSend();
                role_model.openPermanentBanSend();

                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK
            });
        }
    );
};
